home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / vidhrdw / terracre.c < prev    next >
C/C++ Source or Header  |  2000-05-04  |  7KB  |  246 lines

  1. /***************************************************************************
  2.  
  3.   vidhrdw.c
  4.  
  5.   Functions to emulate the video hardware of the machine.
  6.  
  7. ***************************************************************************/
  8.  
  9. #include "driver.h"
  10. #include "vidhrdw/generic.h"
  11.  
  12.  
  13. unsigned char *terrac_videoram;
  14. size_t terrac_videoram_size;
  15. unsigned char terrac_scrolly[2];
  16.  
  17. static struct osd_bitmap *tmpbitmap2;
  18. static unsigned char *dirtybuffer2;
  19.  
  20. static const unsigned char *spritepalettebank;
  21.  
  22.  
  23. /***************************************************************************
  24.   Convert color prom.
  25. ***************************************************************************/
  26.  
  27. void terrac_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
  28. {
  29.     int i;
  30.     #define TOTAL_COLORS(gfxn) (Machine->gfx[gfxn]->total_colors * Machine->gfx[gfxn]->color_granularity)
  31.     #define COLOR(gfxn,offs) (colortable[Machine->drv->gfxdecodeinfo[gfxn].color_codes_start + offs])
  32.  
  33.  
  34.     for (i = 0;i < Machine->drv->total_colors;i++)
  35.     {
  36.         int bit0,bit1,bit2,bit3;
  37.  
  38.         bit0 = (color_prom[0] >> 0) & 0x01;
  39.         bit1 = (color_prom[0] >> 1) & 0x01;
  40.         bit2 = (color_prom[0] >> 2) & 0x01;
  41.         bit3 = (color_prom[0] >> 3) & 0x01;
  42.         *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  43.         bit0 = (color_prom[Machine->drv->total_colors] >> 0) & 0x01;
  44.         bit1 = (color_prom[Machine->drv->total_colors] >> 1) & 0x01;
  45.         bit2 = (color_prom[Machine->drv->total_colors] >> 2) & 0x01;
  46.         bit3 = (color_prom[Machine->drv->total_colors] >> 3) & 0x01;
  47.         *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  48.         bit0 = (color_prom[2*Machine->drv->total_colors] >> 0) & 0x01;
  49.         bit1 = (color_prom[2*Machine->drv->total_colors] >> 1) & 0x01;
  50.         bit2 = (color_prom[2*Machine->drv->total_colors] >> 2) & 0x01;
  51.         bit3 = (color_prom[2*Machine->drv->total_colors] >> 3) & 0x01;
  52.         *(palette++) = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  53.  
  54.         color_prom++;
  55.     }
  56.  
  57.     color_prom += 2*Machine->drv->total_colors;
  58.     /* color_prom now points to the beginning of the lookup table */
  59.  
  60.  
  61.     /* characters use colors 0-15 */
  62.     for (i = 0;i < TOTAL_COLORS(0);i++)
  63.         COLOR(0,i) = i;
  64.  
  65.     /* background tiles use colors 192-255 in four banks */
  66.     /* the bottom two bits of the color code select the palette bank for */
  67.     /* pens 0-7; the top two bits for pens 8-15. */
  68.     for (i = 0;i < TOTAL_COLORS(1);i++)
  69.     {
  70.         if (i & 8) COLOR(1,i) = 192 + (i & 0x0f) + ((i & 0xc0) >> 2);
  71.         else COLOR(1,i) = 192 + (i & 0x0f) + ((i & 0x30) >> 0);
  72.     }
  73.  
  74.     /* sprites use colors 128-191 in four banks */
  75.     /* The lookup table tells which colors to pick from the selected bank */
  76.     /* the bank is selected by another PROM and depends on the top 8 bits of */
  77.     /* the sprite code. The PROM selects the bank *separately* for pens 0-7 and */
  78.     /* 8-15 (like for tiles). */
  79.     for (i = 0;i < TOTAL_COLORS(2)/16;i++)
  80.     {
  81.         int j;
  82.  
  83.         for (j = 0;j < 16;j++)
  84.         {
  85.             if (i & 8)
  86.                 COLOR(2,i + j * (TOTAL_COLORS(2)/16)) = 128 + ((j & 0x0c) << 2) + (*color_prom & 0x0f);
  87.             else
  88.                 COLOR(2,i + j * (TOTAL_COLORS(2)/16)) = 128 + ((j & 0x03) << 4) + (*color_prom & 0x0f);
  89.         }
  90.  
  91.         color_prom++;
  92.     }
  93.  
  94.     /* color_prom now points to the beginning of the sprite palette bank table */
  95.     spritepalettebank = color_prom;    /* we'll need it at run time */
  96. }
  97.  
  98.  
  99.  
  100. WRITE_HANDLER( terrac_videoram2_w )
  101. {
  102.     int oldword = READ_WORD(&terrac_videoram[offset]);
  103.     int newword = COMBINE_WORD(oldword,data);
  104.  
  105.     if (oldword != newword)
  106.     {
  107.         WRITE_WORD(&terrac_videoram[offset],newword);
  108.         dirtybuffer2[offset] = 1;
  109.         dirtybuffer2[offset+1] = 1;
  110.     }
  111. }
  112.  
  113. READ_HANDLER( terrac_videoram2_r )
  114. {
  115.    return READ_WORD (&terrac_videoram[offset]);
  116. }
  117.  
  118.  
  119. /***************************************************************************
  120.   Stop the video hardware emulation.
  121. ***************************************************************************/
  122.  
  123. void terrac_vh_stop(void)
  124. {
  125.         free(dirtybuffer2);
  126.     osd_free_bitmap(tmpbitmap2);
  127.     generic_vh_stop();
  128. }
  129.  
  130. /***************************************************************************
  131.   Start the video hardware emulation.
  132. ***************************************************************************/
  133.  
  134.  
  135. int terrac_vh_start(void)
  136. {
  137.     if (generic_vh_start() != 0)
  138.         return 1;
  139.  
  140.     if ((dirtybuffer2 = malloc(terrac_videoram_size)) == 0)
  141.     {
  142.         terrac_vh_stop();
  143.         return 1;
  144.     }
  145.     memset(dirtybuffer2,1,terrac_videoram_size);
  146.  
  147.     /* the background area is 4 x 1 (90 Rotated!) */
  148.     if ((tmpbitmap2 = osd_new_bitmap(4*Machine->drv->screen_width,
  149.             1*Machine->drv->screen_height,Machine->scrbitmap->depth)) == 0)
  150.     {
  151.         terrac_vh_stop();
  152.         return 1;
  153.     }
  154.  
  155.     return 0;
  156. }
  157.  
  158.  
  159.  
  160. /***************************************************************************
  161.  
  162.   Draw the game screen in the given osd_bitmap.
  163.   Do NOT call osd_update_display() from this function, it will be called by
  164.   the main emulation engine.
  165.  
  166. ***************************************************************************/
  167. void terracre_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
  168. {
  169.     int offs,x,y;
  170.  
  171.  
  172.     for (y = 0; y < 64; y++)
  173.     {
  174.         for (x = 0; x < 16; x++)
  175.         {
  176.             if ((dirtybuffer2[x*2 + y*64]) || (dirtybuffer2[x*2 + y*64+1]))
  177.             {
  178.                 int code = READ_WORD(&terrac_videoram[x*2 + y*64]) & 0x01ff;
  179.                 int color = (READ_WORD(&terrac_videoram[x*2 + y*64])&0x7800)>>11;
  180.  
  181.                 dirtybuffer2[x*2 + y*64] = dirtybuffer2[x*2 + y*64+1] = 0;
  182.  
  183.                 drawgfx(tmpbitmap2,Machine->gfx[1],
  184.                         code,
  185.                         color,
  186.                         0,0,
  187.                         16 * y,16 * x,
  188.                         0,TRANSPARENCY_NONE,0);
  189.             }
  190.         }
  191.     }
  192.  
  193.     /* copy the background graphics */
  194.     if (READ_WORD(terrac_scrolly) & 0x2000)    /* background disable */
  195.         fillbitmap(bitmap,Machine->pens[0],&Machine->drv->visible_area);
  196.     else
  197.     {
  198.         int scrollx;
  199.  
  200.         scrollx = -READ_WORD(terrac_scrolly);
  201.  
  202.         copyscrollbitmap(bitmap,tmpbitmap2,1,&scrollx,0,0,&Machine->drv->visible_area,TRANSPARENCY_NONE,0);
  203.     }
  204.  
  205.  
  206.  
  207.     for (x = 0;x <spriteram_size;x += 8)
  208.     {
  209.         int code;
  210.         int attr = READ_WORD(&spriteram[x+4]) & 0xff;
  211.         int color = (attr & 0xf0) >> 4;
  212.         int flipx = attr & 0x04;
  213.         int flipy = attr & 0x08;
  214.         int sx,sy;
  215.  
  216.         sx = (READ_WORD(&spriteram[x+6]) & 0xff) - 0x80 + 256 * (attr & 1);
  217.         sy = 240 - (READ_WORD(&spriteram[x]) & 0xff);
  218.  
  219.         code = (READ_WORD(&spriteram[x+2]) & 0xff) + ((attr & 0x02) << 7);
  220.  
  221.         drawgfx(bitmap,Machine->gfx[2],
  222.                 code,
  223.                 color + 16 * (spritepalettebank[code >> 1] & 0x0f),
  224.                 flipx,flipy,
  225.                 sx,sy,
  226.                 &Machine->drv->visible_area,TRANSPARENCY_PEN,0);
  227.     }
  228.  
  229.  
  230.     for (offs = videoram_size - 2;offs >= 0;offs -= 2)
  231.     {
  232.         int sx,sy;
  233.  
  234.  
  235.         sx = (offs/2) / 32;
  236.         sy = (offs/2) % 32;
  237.  
  238.         drawgfx(bitmap,Machine->gfx[0],
  239.                 READ_WORD(&videoram[offs]) & 0xff,
  240.                 0,
  241.                 0,0,
  242.                 8*sx,8*sy,
  243.                 &Machine->drv->visible_area,TRANSPARENCY_PEN,15);
  244.     }
  245. }
  246.